var util = require('../../utils/util.js');
//获取应用实例
const app = getApp();
Page({
    data: {
        //tabbar
        tabbar: {},
        pagenum: 1,
        pageData: {
            to: 0,
            total: 1
        },
        motto: 'Hello World',
        userInfo: {},
        hasUserInfo: false,
        lingPaiNum: '',
        totalData: []
    },
    // // 截取时间
    // slice: function(model){
    //   return model.slice(1,-3);
    // },
    onShow: function() {
		
        //隐藏系统tabbar
        wx.hideTabBar();
        // var that = this;
        // if (wx.pageScrollTo) { //回到顶部
        //   wx.pageScrollTo({
        //     scrollTop: 0
        //   })
        // }
        // that.setData({
        //   isLoadEnd: false,
        //   isHideLoadMore: false,
        //   pagenum: 1,
        //   pageData: {
        //     to: 0,
        //     total: 1
        //   }
        // });

    },
    onLoad: function(index) {
        app.editTabbar();
		
        var that = this;
        //获取 首页列表
        util.wGet('activity', {}).then((res) => {
            // this.data.listData = res.data.data.data;
            var resData = res.data.data.data;

            for (var i = 0; i < resData.length; i++) {
                resData[i].open_at = resData[i].open_at.slice(0, -3);
            }
            that.setData({
                totalData: resData
            });
            if (that.data.totalData.length < 4) {
                that.setData({
                    isHideLoadMore: true,
                    isLoadEnd: true,
                })
            }
            if (that.data.totalData == '') {
                wx.showToast({
                    icon: "none",
                    title: "暂无记录"
                })
                that.setData({
                    isHideLoadMore: true,
                    isLoadEnd: true
                })
            }
			
        })

        if (app.globalData.userInfo) {
            this.setData({
                userInfo: app.globalData.userInfo,
                hasUserInfo: true
            })
        } else if (this.data.canIUse) {
            // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
            // 所以此处加入 callback 以防止这种情况
            app.userInfoReadyCallback = res => {
                this.setData({
                    userInfo: res.userInfo,
                    hasUserInfo: true
                })
            }
        } else {
            // 在没有 open-type=getUserInfo 版本的兼容处理
            wx.getUserInfo({
                success: res => {
                    app.globalData.userInfo = res.userInfo
                    this.setData({
                        userInfo: res.userInfo,
                        hasUserInfo: true
                    })
                }
            })
        }
		if (index.key) {
			var index = index.key;
			util.wGet('activity/info', { activity_id: index }).then((res) => {
				if (res.data.code == 1 || res.data.data == '') {
					wx.showToast({
						icon: "none",
						title: "该活动已不存在"
					})
					//获取 首页列表
					util.wGet('activity', {}).then((res) => {
						var resData = res.data.data.data;
						for (var i = 0; i < resData.length; i++) {
							resData[i].open_at = resData[i].open_at.slice(0, -3);
						}
						that.setData({
							totalData: resData
						});
						if (that.data.totalData.length < 4) {
							that.setData({
								isHideLoadMore: true,
								isLoadEnd: true,
							})
						}
						if (that.data.totalData == '') {
							wx.showToast({
								icon: "none",
								title: "暂无记录"
							})
							that.setData({
								isHideLoadMore: true,
								isLoadEnd: true
							})
						}

					})
				} else {
					wx.navigateTo({
						url: '../activity/activity?key=' + index
					})
				}
			});
		}


    },
    // 下拉刷新 
    onPullDownRefresh: function() {
        wx.setNavigationBarTitle({
            title: '刷新中……'
        })
        var that = this;
        that.setData({
            isHideLoadMore: false,
            isLoadEnd: false,
            pagenum: 1,
            pageData: {
                to: 0,
                total: 1
            }
        })
        //获取 首页列表
        util.wGet('activity', {}).then((res) => {
			for (var i = 0; i < res.data.data.data.length; i++) {
				res.data.data.data[i].open_at = res.data.data.data[i].open_at.slice(0, -3);
			}
            // this.data.listData = res.data.data.data;
            that.setData({
                totalData: res.data.data.data
            });
        })
        wx.setNavigationBarTitle({
            title: '抽奖助手'
        })
        wx.stopPullDownRefresh();
    },
    // 上拉加载
    onReachBottom: function() {
        var that = this;
        var pagenum = that.data.pagenum;
        var toNum = that.data.pageData.to;
        var totalNum = that.data.pageData.total;
        if (toNum != totalNum) {
            pagenum += 1;
            that.setData({
                pagenum: pagenum
            })
            //获取分页
            util.wGet('activity', {
                page: that.data.pagenum
            }).then((res) => {
                var resData = res.data.data.data;
                for (var i = 0; i < resData.length; i++) {
                    resData[i].open_at = resData[i].open_at.slice(0, -3);
                }
                if (resData == '') {
                    that.setData({
                        isHideLoadMore: true,
                        isLoadEnd: true
                    })
                }
                var toStr = 'pageData.to';
                var totalStr = 'pageData.total';
                that.setData({
                    [toStr]: res.data.data.to,
                    [totalStr]: res.data.data.total
                })
                for (var i = 0; i < resData.length; i++) {
                    that.data.totalData.push(resData[i])
                }
                that.setData({
                    totalData: that.data.totalData
                })
            })
        } else {
            that.setData({
                isHideLoadMore: true,
                isLoadEnd: true
            })
        }
    },
    // // 获取数据
    // getData: function() {
    //   var that = this;
    //   that.setData({
    //     pagenum: 1
    //   })
    //   var num = wx.getStorageSync('lingPaiStr');
    //   wx.request({
    //     url: 'https://wechat.wuyuzhong.com/api/activity',
    //     data: {
    //       page: that.data.pagenum
    //     },
    //     header: {
    //       'Content-Type': 'application/json',
    //       'Accept': 'application/json',
    //       Authorization: num
    //     },
    //     success: function(res) {
    //       that.setData({
    //         totalData: res.data.data.data
    //       })
    //     }
    //   })
    // },
    showActivity: function(e) {
        var key = e.currentTarget.dataset.key;
        wx.navigateTo({
            url: '../activity/activity?key=' + key
        })
    }
})